﻿using static CyberU;
using static CyberU.UI;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System;

public class MusicItem : ClickTextSelectItem
{
    public ClickText item;
    public AudioSource audioSource;
    public bool playOnAwake = false;

    public string path
    {
        get{ return filePath;}
        set{ filePath = value;}
    }
    public string filePath = @"S:\autom\repositories\HISHIGHNESS\repositories\Collection\musics\原声\Italia e voi.mp3";
    public bool loop = false;
    public bool isPlaying = false;
    public bool hasPlayed = false;
    public MusicPlayer parent;
    public int index;

    void Start()
    {
        // gameObject.SetActive(true);
        // Delog(filePath);
        StartCoroutine(AppendAudioSource(filePath, parent: gameObject, loop: loop, onAudioSourceReady: OnAudioSourceReady, playOnAwake: playOnAwake));
        // gameObject.SetActive(false);
    }

    void OnAudioSourceReady(AudioSource source, string filePath)
    {
        // Delog($"audio source ready. {filePath}");
        audioSource = source;
        this.filePath = filePath;

        string fileName = System.IO.Path.GetFileNameWithoutExtension(filePath);
        // item.text = fileName;
    }

    public void Pause()
    {
        if (isPlaying)
        {
            audioSource.Pause();
            isPlaying = false;
            // Debug.Log("Music item paused");
        }
    }
    public void Stop()
    {
        if (isPlaying)
        {
            audioSource.Stop();
            isPlaying = false;
            // Debug.Log("Music item paused");
        }
    }

    public void ReStart()
    {
        if (hasPlayed)
        {
            Stop();
            Play();
        }
    }

    public void Play()
    {
        if (!isPlaying)
        {
            isPlaying = true;
            audioSource.Play();
            hasPlayed = true;
        }
    }
    public void ToggleAudioPlaying()
    {
        if (!hasPlayed)
        {
            Play();
            // Debug.Log("First play started.");
        }
        else
        {
            if (isPlaying)
            {
                audioSource.Pause();
            }
            else
            {
                audioSource.Play();
            }
        }

        isPlaying = !isPlaying;
    }

    public static MusicItem CreateMusicItem(MusicPlayer parent ,int index,string path,bool playOnAwake=false,bool loop=false)
    {
        GameObject musicItemPrefab = Resources.Load<GameObject>("Prefabs/MusicItem");
        // Delog($"{Used(parent.selector)}");
        GameObject gameObject = Instantiate(musicItemPrefab, parent.songsSelector.transform);
        MusicItem musicItem = gameObject.GetComponent<MusicItem>();
        musicItem.parent = parent;
        musicItem.index = index;
        musicItem.item = gameObject.AddComponent<ClickText>();
        
        musicItem.filePath = path;
        musicItem.playOnAwake = playOnAwake;
        musicItem.loop = loop;

        // AddRect(component: musicItem, color: Color.green);
        return musicItem;
    }
}